﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConverterInterface;

namespace CSharpConverterLibrary
{
    public class NothingState : State
    {
        private NothingState()  { }

        private static NothingState instance = new NothingState();

        public static NothingState Instance
        {
            get {return instance;}
        }

        private char c;

        public override void Execute(CSharpConverter owner)
        {
            if (owner.AvailableChars() <= 0)
            {
                owner.machine.Finished = true;
                owner.FlushBuffer();
            }
            else
            {
                c = owner.Read();
                
                if (char.IsLetter(c) || c == '_' || char.IsDigit(c))  // -> identifier
                {
                    owner.machine.ChangeState(IdentifierState.Instance);
                }
                    
                //else if (char.IsDigit(c)) // -> number
                //{
                //    owner.machine.ChangeState(NumberState.Instance);
                //}

                else if (c == '\"') // -> string
                {
                    owner.machine.ChangeState(StringState.Instance);
                }

                else if (c == '\'') // -> char
                {
                    owner.machine.ChangeState(CharState.Instance);
                }

                else if (c == '/') // -> commentary ?
                {
                    if (owner.AvailableChars() > 0)
                    {
                        if (owner.Peek() == '/')
                        {
                            owner.machine.ChangeState(SinglelineCommentState.Instance);
                            owner.machine.Buffer.Append(c);
                            owner.Forward();
                        }
                        else if (owner.Peek() == '*')
                        {
                            owner.machine.ChangeState(MultilineCommentState.Instance);
                            owner.machine.Buffer.Append(owner.Read());
                            
                        }
                    }
                }

                else // -> continue nothing
                {
                    owner.machine.Buffer.Append(c);
                }

            }

        }

        public override void Exit(CSharpConverter owner)
        {
            owner.machine.Buffer.Replace("\"", "&quot;");
            owner.machine.Buffer.Replace("<", "&lt;");
            owner.machine.Buffer.Replace(">", "&gt;");
            owner.FlushBuffer();
            owner.machine.Buffer.Append(c);
        }
    }
}
